package me.ui;

import me.domain.User;
import me.util.CodeUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;

public class LoginJFrame extends JFrame implements MouseListener {

    static ArrayList<User> list=new ArrayList<>();
    static {;
        list.add(new User("zhangsan","123"));
    }
    static{
        list.add(new User("lisi","456"));
    }

    JButton login=new JButton();
    JButton register=new JButton();
    JTextField username=new JTextField();
    JPasswordField password=new JPasswordField();
    JTextField code=new JTextField();

    JLabel right=new JLabel();


    public LoginJFrame() {
       initJFrame();
       initView();
       this.setVisible(true);
    }

    private void initJFrame() {
        this.setSize(488,430);
        this.setTitle("拼图游戏 V1.0登录");
        this.setDefaultCloseOperation(3);
        this.setLocationRelativeTo(null);
        this.setAlwaysOnTop(true);
        this.setLayout(null);
    }

    private void initView() {
        JLabel usernameText=new JLabel(new ImageIcon("day14\\image\\login\\用户名.png"));
        usernameText.setBounds(116,135,47,17);
        this.getContentPane().add(usernameText);

        username.setBounds(195,134,200,30);
        this.getContentPane().add(username);

        JLabel passwordText=new JLabel(new ImageIcon("day14\\image\\login\\密码.png"));
        passwordText.setBounds(130,195,32,16);
        this.getContentPane().add(passwordText);

        password.setBounds(195,195,200,30);
        this.getContentPane().add(password);

        JLabel codeText=new JLabel(new ImageIcon("day14\\image\\login\\验证码.png"));
        codeText.setBounds(133,256,50,30);
        this.getContentPane().add(codeText);

        code.setBounds(195,256,100,30);
        this.getContentPane().add(code);

        String codeStr= CodeUtil.getCode();
        JLabel rightCode=new JLabel(codeStr);
        rightCode.setText(codeStr);
        rightCode.addMouseListener(this);
        rightCode.setBounds(300,256,50,30);
        this.getContentPane().add(rightCode);

        login.setBounds(123,310,128,47);
       login.setIcon(new ImageIcon("day14\\image\\login\\登录按钮.png"));
       login.setBorderPainted(false);
       login.setContentAreaFilled(false);
       login.addMouseListener(this);
       this.getContentPane().add(login);

       register.setBounds(256,310,128,47);
       register.setIcon(new ImageIcon("day14\\image\\login\\注册按钮.png"));
       register.setBorderPainted(false);
       register.setContentAreaFilled(false);
       register.addMouseListener(this);
       this.getContentPane().add(register);

       JLabel background=new JLabel(new ImageIcon("day14\\image\\login\\background.png"));
       background.setBounds(0,0,470,390);
       this.getContentPane().add(background);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        Label rightCode = null;
        if (e.getSource() == login) {
            System.out.println("点击了登录按钮");
            //获取两个文本输入框中的内容
            String usernameInput = username.getText();
            String passwordInput = password.getText();
            //获取用户输入的验证码
            String codeInput = code.getText();

            User userInfo = new User(usernameInput, passwordInput);
            System.out.println("用户输入的用户名为" + usernameInput);
            System.out.println("用户输入的密码为" + passwordInput);
            rightCode = null;
            if (codeInput.length() == 0) {
                showJDialog("验证码不能为空");
            } else if (usernameInput.length() == 0 || passwordInput.length() == 0) {
                //校验用户名和密码是否为空
                System.out.println("用户名或者密码为空");

                //调用showJDialog方法并展示弹框
                showJDialog("用户名或者密码为空");


            } else if (!codeInput.equalsIgnoreCase(rightCode.getText())) {
                showJDialog("验证码输入错误");
            } else if (contains(userInfo)) {
                System.out.println("用户名和密码正确可以开始玩游戏了");
                //关闭当前登录界面
                this.setVisible(false);
                //打开游戏的主界面
                //需要把当前登录的用户名传递给游戏界面
                new GameJFrame();
            } else {
                System.out.println("用户名或密码错误");
                showJDialog("用户名或密码错误");
            }
        } else if (e.getSource() == register) {
            System.out.println("点击了注册按钮");
        } else if (e.getSource() == rightCode) {
            System.out.println("更换验证码");
            //获取一个新的验证码
            String code = CodeUtil.getCode();
            rightCode.setText(code);
        }
    }

    private void showJDialog(String 验证码不能为空) {
        JDialog jDialog = new JDialog();
        //给弹框设置大小
        jDialog.setSize(200, 150);
        //让弹框置顶
        jDialog.setAlwaysOnTop(true);
        //让弹框居中
        jDialog.setLocationRelativeTo(null);
        //弹框不关闭永远无法操作下面的界面
        jDialog.setModal(true);

        //创建Jlabel对象管理文字并添加到弹框当中
        String content = "";
        JLabel warning = new JLabel(content);
        warning.setBounds(0, 0, 200, 150);
        jDialog.getContentPane().add(warning);

        //让弹框展示出来
        jDialog.setVisible(true);
    }

    @Override
    public void mousePressed(MouseEvent e) {
        if (e.getSource() == login) {
            login.setIcon(new ImageIcon("day14\\image\\login\\登录按钮.png"));
        } else if (e.getSource() == register) {
            register.setIcon(new ImageIcon("day14\\image\\login\\注册按钮.png"));
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

}
